package com.atguigu.tingshu.dispatch.job;

import com.atguigu.tingshu.dispatch.mapper.XxlJobLogMapper;
import com.atguigu.tingshu.model.dispatch.XxlJobLog;
import com.atguigu.tingshu.search.client.SearchFeignClient;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DispatchJobHandler {

    @Autowired
    private SearchFeignClient searchFeignClient;

    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private XxlJobLogMapper xxlJobLogMapper;

    /**
     * 更新排行榜
     */
    @XxlJob("updateLatelyAlbumRankingJob")
    public void updateLatelyAlbumRankingJob(){
        //  声明一个对象
        XxlJobLog xxlJobLog = new XxlJobLog();
        try {
            //  定义变量开始时间.
            long startTime = System.currentTimeMillis();
            xxlJobLog.setJobId(XxlJobHelper.getJobId());
            //  调用更新排行榜
            searchFeignClient.updateLatelyAlbumRanking();
            xxlJobLog.setStatus(1);
            //  结束时间
            Long times = System.currentTimeMillis()- startTime;
            xxlJobLog.setTimes(times.intValue());
        } catch (Exception e) {
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(e.getMessage());
            //  throw new RuntimeException(e);
        }
        //  保存到数据库
        xxlJobLogMapper.insert(xxlJobLog);
    }

    /**
     * 更新vip状态
     */
    @XxlJob("updateVipExpireStatusJob")
    public void updateVipExpireStatusJob(){
        //  声明一个对象
        XxlJobLog xxlJobLog = new XxlJobLog();
        try {
            //  定义变量开始时间.
            long startTime = System.currentTimeMillis();
            xxlJobLog.setJobId(XxlJobHelper.getJobId());
            //  调用更新排行榜
            userInfoFeignClient.updateVipExpireStatus();
            xxlJobLog.setStatus(1);
            //  结束时间
            Long times = System.currentTimeMillis()- startTime;
            xxlJobLog.setTimes(times.intValue());
        } catch (Exception e) {
            xxlJobLog.setStatus(0);
            xxlJobLog.setError(e.getMessage());
            //  throw new RuntimeException(e);
        }
        //  保存到数据库
        xxlJobLogMapper.insert(xxlJobLog);
    }
}